package com.example.demo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RequestMapping("/user")
@RestController
public class registerController {
    @RequestMapping("/login")
    public boolean loginIn(String userName, String password, HttpSession session){

       /* if(userName ==null || userName.length() == 0
                ||password == null || password.length() == 0){
            System.out.println("登录失败");
            return false;
        }*/
        //StringUtils.hasLength相当于封装判断长度的功能
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //判断密码是否正确
        if("admin".equals(password) && "admin".equals(userName)){
            //设置sessionId
            session.setAttribute("userName",userName);
            return  true;
        }
        return false;
    }
    @RequestMapping("/index")
    public String getUseName(@SessionAttribute("userName") String userName){
        return userName;
    }
}
